<?php

declare(strict_types=1);

namespace App\Admin;

use App\Form\Type\SettingAvatarFieldType;
use App\Form\Type\SettingImageFieldType;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;

final class SettingAdmin extends AbstractAdmin {
    protected $baseRouteName = 'admin_setting';
    protected $baseRoutePattern = 'setting';

    protected function configureDatagridFilters (DatagridMapper $datagridMapper): void {
        $datagridMapper
            ->add('id')
            ->add('seo_keys')
            ->add('seo_description')
            ->add('site_name')
            ->add('site_url')
            ->add('introduce')
            ->add('phone')
            ->add('email')
            ->add('address')
            ->add('social_qq')
            ->add('social_wechat')
            ->add('social_weibo')
            ->add('social_qzone');
    }

    protected function configureListFields (ListMapper $listMapper): void {
        $listMapper
            ->add('id')
            ->add('seo_keys')
            ->add('seo_description')
            ->add('site_name')
            ->add('site_url')
            ->add('introduce')
            ->add('phone')
            ->add('email')
            ->add('address')
            ->add('social_qq')
            ->add('social_wechat')
            ->add('social_weibo')
            ->add('social_qzone')
            ->add('_action', null, [
                'actions' => [
                    'show'   => [],
                    'edit'   => [],
                    'delete' => [],
                ],
            ]);
    }

    protected function configureFormFields (FormMapper $formMapper): void {
        $formMapper
            ->tab('系统信息')
                ->with('基础')
                    ->add('site_name', null, ['label' => '站点名称'])
                    ->add('site_url', null, ['label' => '站点地址'])
                    ->add('seo_keys',null,[
                        'label' => 'SEO 关键词',
                        'help'  => '多个关键词请以逗号","隔开'
                    ])
                    ->add('seo_description', null, ['label' => 'SEO 描述'])
                    ->add('dashboard_type', ChoiceType::class, [
                        'label'    => '控制面板',
                        'choices' => [
                            '经典'  => 'classics',
                            '工作台' => 'workbench',
                        ],
                        'help'     => '控制面板展现的风格，默认为经典风格',
                        'required' => false,
                    ])
                    ->add('is_comment', null, [
                        'label' => '开启评论',
                        'help'  => '开启后将启用评论功能，会结合文章评论设置一起使用'
                    ])
                    ->add('is_auto_pass_comment', null, [
                        'label' => '评论审核',
                        'help'  => '开启后将自动通过评论审核，否则需要自行审核'
                    ])
                    ->add('is_subscribe', null, [
                        'label' => '订阅通知',
                        'help'  => '开启后将会自动在发布新文章时给订阅用户发送邮件通知'
                    ])
                    ->add('is_reply', null, [
                        'label' => '回复通知',
                        'help'  => '开启后将会自动对被评论用户发送邮件通知'
                    ])
                    ->add('is_maintenance', null, [
                        'label' => '站点维护',
                        'help'  => '开启后前台就进入维护模式'
                    ])
                ->end()
            ->end()
            ->tab('我的信息')
                ->with('关于我', ['class' => 'col-md-8'])
                    ->add('name', null, ['label' => '姓名'])
                    ->add('introduce', null, ['label' => '简介'])
                    ->add('description', TextareaType::class, [
                        'label' => '详细',
                        'required' => false,
                        'attr'  => ['style' => 'resize:none;', 'rows' => 4]
                    ])
                    ->add('phone', null, ['label' => '手机号'])
                    ->add('email', null, ['label' => '邮箱'])
                    ->add('address', null, ['label' => '地址'])
                ->end()
                ->with('其他', ['class' => 'col-md-4'])
                    ->add('avatar', HiddenType::class)
                    ->add('avatar_item', SettingAvatarFieldType::class, ['label' => '头像'])
                    ->add('image', HiddenType::class)
                    ->add('image_item', SettingImageFieldType::class, ['label' => '图片'])
                ->end()
            ->end()
            ->tab('社交信息')
                ->with('社交平台')
                    ->add('social_qq', null, ['label' => 'QQ'])
                    ->add('social_wechat', null, ['label' => '微信'])
                    ->add('social_weibo', null, ['label' => '微博'])
                    ->add('social_qzone', null, ['label' => 'GitHub'])
                ->end()
            ->end();
    }

    protected function configureShowFields (ShowMapper $showMapper): void {
        $showMapper
            ->add('id')
            ->add('seo_keys')
            ->add('seo_description')
            ->add('site_name')
            ->add('site_url')
            ->add('introduce')
            ->add('phone')
            ->add('email')
            ->add('address')
            ->add('social_qq')
            ->add('social_wechat')
            ->add('social_weibo')
            ->add('social_qzone');
    }

    protected function configureRoutes (RouteCollection $collection) {
        parent::configureRoutes($collection); // TODO: Change the autogenerated stub
        $collection->clearExcept(['list','edit']);
    }

    public function getTemplate ($name) {
        switch ($name) {
            case 'edit':
                return 'backend/setting/edit.html.twig';
            default:
                return $this->getTemplateRegistry()->getTemplate($name);
        }
    }
}
